package game.module.qzmj.dao;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.google.common.io.Files;
import game.module.qzmj.bean.QzmjRoomCfg;
import lion.common.Reloadable;

public class QzmjRoomTemplateCache implements Reloadable {
	 private static Logger logger = LoggerFactory.getLogger(QzmjRoomTemplateCache.class);
	    
	    static class SingletonHolder{
	    	static QzmjRoomTemplateCache instance = new QzmjRoomTemplateCache();
	    }
	    public static QzmjRoomTemplateCache getInstance() {
			return SingletonHolder.instance;
		}
	    private volatile List<QzmjRoomCfg> templateList;
		@Override
		public void reload() {
			// TODO Auto-generated method stub
			try{
				String jsonStr = Files.asCharSource(new File("data/mjroom.json"),  Charset.forName("UTF-8")).read();
				//String jsonStr = Files.toString(new File("data/mjroom.json"),  Charset.forName("UTF-8"));
				ObjectMapper mapper = new ObjectMapper();
				templateList = mapper.readValue(jsonStr, new TypeReference<List<QzmjRoomCfg>>() {});
				logger.info("size={}", templateList.size());
			}catch(Exception e){
				logger.error("",e);
			}
		}
		
		public boolean containsConfigIndex(int cfgIndex) {
			return cfgIndex < templateList.size();
		}
		public QzmjRoomCfg getTemplate(int cfgIndex) {
			return (QzmjRoomCfg) (templateList.get(cfgIndex));
		}

}
